package cn.orange.ch03_linked;

import cn.orange.ListNode;

/**
 * LC160.链表相交
 给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点，返回 null
 */
public class LC160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pA = headA, pB = headB;
        while (pA != pB) {
            if (pA == null) {
                pA = headB;
            } else {
                pA = pA.next;
            }
            if (pB == null) {
                pB = headA;
            } else {
                pB = pB.next;
            }
        }
        return pA;
    }
    public static void main(String[] args) {
        LC160 alg = new LC160();
        ListNode h1 = new ListNode(3);
        ListNode h2 = new ListNode(2);
        h2.next = h1;
        System.out.println(alg.getIntersectionNode(h1, h2));
    }
}
